/**
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2019-2020 十三 all rights reserved.
 * 版权所有，侵权必究！
 */
package ltd.wood.mall.service.impl;

import ltd.wood.mall.common.Constants;
import ltd.wood.mall.common.ServiceResultEnum;
import ltd.wood.mall.controller.vo.WoodMallShoppingCartItemVO;
import ltd.wood.mall.dao.WoodMallGoodsMapper;
import ltd.wood.mall.dao.WoodMallShoppingCartItemMapper;
import ltd.wood.mall.entity.WoodMallGoods;
import ltd.wood.mall.entity.WoodMallShoppingCartItem;
import ltd.wood.mall.service.WoodMallShoppingCartService;
import ltd.wood.mall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class WoodMallShoppingCartServiceImpl implements WoodMallShoppingCartService {

    @Autowired
    private WoodMallShoppingCartItemMapper woodMallShoppingCartItemMapper;

    @Autowired
    private WoodMallGoodsMapper woodMallGoodsMapper;

    @Override
    public String saveWoodMallCartItem(WoodMallShoppingCartItem woodMallShoppingCartItem) {
        WoodMallShoppingCartItem temp = woodMallShoppingCartItemMapper.selectByUserIdAndGoodsId(woodMallShoppingCartItem.getUserId(), woodMallShoppingCartItem.getGoodsId());
        if (temp != null) {
            //已存在则修改该记录
            temp.setGoodsCount(woodMallShoppingCartItem.getGoodsCount());
            return updateWoodMallCartItem(temp);
        }
        WoodMallGoods woodMallGoods = woodMallGoodsMapper.selectByPrimaryKey(woodMallShoppingCartItem.getGoodsId());
        //商品为空
        if (woodMallGoods == null) {
            return ServiceResultEnum.GOODS_NOT_EXIST.getResult();
        }
        int totalItem = woodMallShoppingCartItemMapper.selectCountByUserId(woodMallShoppingCartItem.getUserId()) + 1;
        //超出单个商品的最大数量
        if (woodMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //超出最大数量
        if (totalItem > Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_TOTAL_NUMBER_ERROR.getResult();
        }
        //保存记录
        if (woodMallShoppingCartItemMapper.insertSelective(woodMallShoppingCartItem) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateWoodMallCartItem(WoodMallShoppingCartItem woodMallShoppingCartItem) {
        WoodMallShoppingCartItem woodMallShoppingCartItemUpdate = woodMallShoppingCartItemMapper.selectByPrimaryKey(woodMallShoppingCartItem.getCartItemId());
        if (woodMallShoppingCartItemUpdate == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        //超出单个商品的最大数量
        if (woodMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //当前登录账号的userId与待修改的cartItem中userId不同，返回错误
        if (!woodMallShoppingCartItemUpdate.getUserId().equals(woodMallShoppingCartItem.getUserId())) {
            return ServiceResultEnum.NO_PERMISSION_ERROR.getResult();
        }
        //数值相同，则不执行数据操作
        if (woodMallShoppingCartItem.getGoodsCount().equals(woodMallShoppingCartItemUpdate.getGoodsCount())) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        woodMallShoppingCartItemUpdate.setGoodsCount(woodMallShoppingCartItem.getGoodsCount());
        woodMallShoppingCartItemUpdate.setUpdateTime(new Date());
        //修改记录
        if (woodMallShoppingCartItemMapper.updateByPrimaryKeySelective(woodMallShoppingCartItemUpdate) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public WoodMallShoppingCartItem getWoodMallCartItemById(Long woodMallShoppingCartItemId) {
        return woodMallShoppingCartItemMapper.selectByPrimaryKey(woodMallShoppingCartItemId);
    }

    @Override
    public Boolean deleteById(Long shoppingCartItemId, Long userId) {
        WoodMallShoppingCartItem woodMallShoppingCartItem = woodMallShoppingCartItemMapper.selectByPrimaryKey(shoppingCartItemId);
        if (woodMallShoppingCartItem == null) {
            return false;
        }
        //userId不同不能删除
        if (!userId.equals(woodMallShoppingCartItem.getUserId())) {
            return false;
        }
        return woodMallShoppingCartItemMapper.deleteByPrimaryKey(shoppingCartItemId) > 0;
    }

    @Override
    public List<WoodMallShoppingCartItemVO> getMyShoppingCartItems(Long woodMallUserId) {
        List<WoodMallShoppingCartItemVO> woodMallShoppingCartItemVOS = new ArrayList<>();
        List<WoodMallShoppingCartItem> woodMallShoppingCartItems = woodMallShoppingCartItemMapper.selectByUserId(woodMallUserId, Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER);
        if (!CollectionUtils.isEmpty(woodMallShoppingCartItems)) {
            //查询商品信息并做数据转换
            List<Long> woodMallGoodsIds = woodMallShoppingCartItems.stream().map(WoodMallShoppingCartItem::getGoodsId).collect(Collectors.toList());
            List<WoodMallGoods> woodMallGoods = woodMallGoodsMapper.selectByPrimaryKeys(woodMallGoodsIds);
            Map<Long, WoodMallGoods> woodMallGoodsMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(woodMallGoods)) {
                woodMallGoodsMap = woodMallGoods.stream().collect(Collectors.toMap(WoodMallGoods::getGoodsId, Function.identity(), (entity1, entity2) -> entity1));
            }
            for (WoodMallShoppingCartItem woodMallShoppingCartItem : woodMallShoppingCartItems) {
                WoodMallShoppingCartItemVO woodMallShoppingCartItemVO = new WoodMallShoppingCartItemVO();
                BeanUtil.copyProperties(woodMallShoppingCartItem, woodMallShoppingCartItemVO);
                if (woodMallGoodsMap.containsKey(woodMallShoppingCartItem.getGoodsId())) {
                    WoodMallGoods woodMallGoodsTemp = woodMallGoodsMap.get(woodMallShoppingCartItem.getGoodsId());
                    woodMallShoppingCartItemVO.setGoodsCoverImg(woodMallGoodsTemp.getGoodsCoverImg());
                    String goodsName = woodMallGoodsTemp.getGoodsName();
                    // 字符串过长导致文字超出的问题
                    if (goodsName.length() > 28) {
                        goodsName = goodsName.substring(0, 28) + "...";
                    }
                    woodMallShoppingCartItemVO.setGoodsName(goodsName);
                    woodMallShoppingCartItemVO.setSellingPrice(woodMallGoodsTemp.getSellingPrice());
                    woodMallShoppingCartItemVOS.add(woodMallShoppingCartItemVO);
                }
            }
        }
        return woodMallShoppingCartItemVOS;
    }
}
